package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_mylist;
import com.woke.diyview.XListView;
import com.woke.diyview.XListView.IXListViewListener;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static com.zhongcai.online.R.string.user_balance_money_f4;

public class ZcailistActivity extends Activity implements OnItemClickListener, IXListViewListener {

	private XListView mListView;
	private MyApp application;
	private RelativeLayout mBar;
	private ArrayList<Data_mylist> datalist = new ArrayList<Data_mylist> ();
	private int pageno = 1;//��ѯ��ҳ��
	private Myadapter adapter;
	private String user_balance_money_f;
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	String defalt_df = "0.00";
	DecimalFormat ddf = new DecimalFormat (defalt_df);//保留两位小数

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_zcailist);
		user_balance_money_f = getResources ().getString (user_balance_money_f4);
		intview ();

	}

	@Override
	public void onRefresh() {
		datalist.clear();
		pageno = 1;
		getlistlistdata ();
	}

	@Override
	public void onLoadMore() {
		pageno++;
		getlistlistdata();
	}

	private void intview() {
		application = (MyApp) getApplication ();
		mListView = (XListView) findViewById (R.id.avzcailist_listview);
		mListView.setPullLoadEnable (true);
		mListView.setPullRefreshEnable(true);
		mListView.setXListViewListener (this);
		adapter = new Myadapter ();
		mListView.setAdapter (adapter);
		mBar = (RelativeLayout) findViewById (R.id.avzcailist_progress_bar);

		findViewById (R.id.avzcailist_image_back).setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		findViewById (R.id.avzcailist_text_search).setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent (ZcailistActivity.this, Mylist_checkActivity.class);
				startActivity (intent);
			}
		});
		mListView.setOnItemClickListener (this);
		getlistlistdata ();
	}

	class Myadapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datalist.size ();
		}

		@Override
		public Object getItem(int i) {
			// TODO Auto-generated method stub
			return i;
		}

		@Override
		public long getItemId(int i) {
			// TODO Auto-generated method stub
			return i;
		}

		class Holder {
			TextView mTname;
			TextView mTmoney;
			TextView mTtime;
			TextView mTbalanceMoney;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				holder = new Holder ();
				convertView = getLayoutInflater ().inflate (R.layout.item_adter_mylist, null);
				holder.mTname = (TextView) convertView.findViewById (R.id.item_mylist_name);
				holder.mTmoney = (TextView) convertView.findViewById (R.id.item_mylist_money);
				holder.mTtime = (TextView) convertView.findViewById (R.id.item_mylist_time);
				holder.mTbalanceMoney = (TextView) convertView.findViewById (R.id.item_balance_money);
				convertView.setTag (holder);
			} else {
				holder = (Holder) convertView.getTag ();
			}

			Data_mylist list = datalist.get (position);
			String status = list.status;
			String type = list.type;
			holder.mTname.setText (list.note);
//			if (type.equals ("1")) {
//				holder.mTname.setText ("会员充值");
//			} else if (type.equals ("2")) {
//				holder.mTname.setText ("会员提现");
//			} else if (type.equals ("12")) {
//				holder.mTname.setText ("会员升级");
//			} else if (type.equals ("11")) {
//				holder.mTname.setText ("平台充值");
//			}else if (type.equals ("22")) {
//				holder.mTname.setText ("平台代扣");
//			} else if (type.equals ("3")) {
//				holder.mTname.setText ("中财借贷");
//			} else if (type.equals ("4")) {
//				holder.mTname.setText ("中财理财");
//			} else if (type.equals ("5")) {
//				holder.mTname.setText ("中财支付");
//			} else if (type.equals ("6")) {
//				holder.mTname.setText ("财信通");
//			} else if (type.equals ("7")) {
//				holder.mTname.setText ("好友转账");
//			} else if (type.equals ("71")) {
//				holder.mTname.setText ("平台转账");
//			} else if (type.equals ("8")) {
//				holder.mTname.setText ("中财众筹");
//			} else if (type.equals ("9")) {
//				holder.mTname.setText ("收益");
//			} else if (type.equals ("91")) {
//				holder.mTname.setText ("理财收益");
//			} else if (type.equals ("92")) {
//				holder.mTname.setText ("下线升级");
//			} else if (type.equals ("93")) {
//				holder.mTname.setText ("邀请下级");
//			} else if (type.equals ("41")) {
//				holder.mTname.setText ("活期理财");
//			} else if (type.equals ("42")) {
//				holder.mTname.setText ("定期理财");
//			} else if (type.equals ("94")) {
//				holder.mTname.setText ("下级分润");
//			} else if(type.equals ("51")){
//				holder.mTname.setText ("提现直清");
//			} else if(type.equals ("52")){
//				holder.mTname.setText ("代还充值");
//			} else if(type.equals ("53")){
//				holder.mTname.setText ("代还提现");
//			} else if(type.equals ("95")){
//				holder.mTname.setText ("彩票分润");
//			}else if(type.equals ("100")){
//				holder.mTname.setText ("购买彩票");
//			}else if(type.equals ("101")){
//				holder.mTname.setText ("彩票退款");
//			}else if(type.equals ("102")){
//				holder.mTname.setText ("彩票中奖");
//			}


			holder.mTtime.setText (sdf.format (new Date (list.create_time * 1000L)));
			String mmomey=list.money;
			int endIndex =mmomey.indexOf(".");
			mmomey=mmomey.substring(0,endIndex+3);
			if (status.equals ("1")) {
				holder.mTmoney.setText ("-" + mmomey);
			} else {
				holder.mTmoney.setText ("+" +mmomey);
			}

			ddf.setRoundingMode (RoundingMode.FLOOR);
			String balanceMoney = ddf.format (list.curr_money);
			if (defalt_df.equals (balanceMoney)) {
				//历史数据有的返回0.00
				holder.mTbalanceMoney.setText ("----");
			} else {
				holder.mTbalanceMoney.setText (String.format (user_balance_money_f, balanceMoney));
			}
			return convertView;
		}
	}


	private void getlistlistdata() {
		if (application.getDatas_load () == null) return;
		AsyncHttpClient ahc = new AsyncHttpClient ();
		PersistentCookieStore myCookieStore = new PersistentCookieStore (
				ZcailistActivity.this);
		ahc.setCookieStore (myCookieStore);
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "user");
		params.put ("a", "moneyLoglist");
		params.put("page", pageno);
		params.put("size", "10");
		params.put ("userid", application.getDatas_load ().getId ());
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess (statusCode, headers, response);
				Log.e ("账单", "response" + response);
				try {
					String flag = response.getString ("flag");
					if (flag.equals ("success")) {
						JSONObject jsonObject = response.getJSONObject ("data");
						JSONArray jsonArray = jsonObject.getJSONArray ("data");
						for (int i = 0; i < jsonArray.length (); i++) {
							JSONObject object = jsonArray.getJSONObject (i);
							long create_time = object.getLong ("create_time");
							String money = object.getString ("money");
							String id = object.getString ("id");
							String type = object.getString ("type");
							String status = object.getString ("status");
							String note = object.getString ("note");
							double actual_money = object.getDouble ("actual_money");
							double curr_money = object.getDouble ("curr_money");
							String fee = "";
							if (object.has ("fee")) {
								fee = object.getString ("fee");
							}
							pageno = pageno + 1;
							Data_mylist list = new Data_mylist (create_time, money, id, type, status, note, actual_money, curr_money, fee);
							datalist.add (list);
						}
					} else if (flag.equals ("unknow")) {
						String info = response.getString ("info");
						Toast.makeText (ZcailistActivity.this, info, Toast.LENGTH_SHORT).show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}

				if (datalist.size () > 0) {
					adapter.notifyDataSetChanged ();
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (ZcailistActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show ();
//				Log.e("������ǰ�dd", "ʧ��" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();

				mBar.setVisibility (View.VISIBLE);

			}

			@Override
			public void onFinish() {
				super.onFinish ();
				mListView.stopRefresh();
				mListView.stopLoadMore();
				mBar.setVisibility (View.GONE);
//				Log.e("������ǰ�", "���");
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		Intent intent = new Intent (ZcailistActivity.this, Zcailist_infoActivity.class);
		intent.putExtra ("id", datalist.get (i - 1).id);
		startActivity (intent);
	}
}
